agora inbox for pgsql-hackers@postgresql.orghelp / color / mirror / Atom feed
[PATCH v7 1/3] Add OR REPLACE option to CREATE MATERIALIZED VIEW 513+ messages / 2 participants [nested] [flat]
* [PATCH v7 1/3] Add OR REPLACE option to CREATE MATERIALIZED VIEW @ 2024-05-21 16:35 Erik Wienhold <ewie@ewie.name> 0 siblings, 0 replies; 513+ messages in thread From: Erik Wienhold @ 2024-05-21 16:35 UTC (permalink / raw) --- .../sgml/ref/create_materialized_view.sgml | 15 +- src/backend/commands/createas.c | 219 ++++++++++++++---- src/backend/commands/tablecmds.c | 8 +- src/backend/commands/view.c | 106 ++++++--- src/backend/parser/gram.y | 15 ++ src/bin/psql/tab-complete.in.c | 26 ++- src/include/commands/view.h | 3 + src/include/nodes/parsenodes.h | 2 +- src/include/nodes/primnodes.h | 1 + src/test/regress/expected/matview.out | 205 ++++++++++++++++ src/test/regress/sql/matview.sql | 117 ++++++++++ 11 files changed, 624 insertions(+), 93 deletions(-) diff --git a/doc/src/sgml/ref/create_materialized_view.sgml b/doc/src/sgml/ref/create_materialized_view.sgml index 62d897931c3..5e03320eb73 100644 --- a/doc/src/sgml/ref/create_materialized_view.sgml +++ b/doc/src/sgml/ref/create_materialized_view.sgml @@ -21,7 +21,7 @@ PostgreSQL documentation <refsynopsisdiv> <synopsis> -CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] <replaceable>table_name</replaceable> +CREATE [ OR REPLACE ] MATERIALIZED VIEW [ IF NOT EXISTS ] <replaceable>table_name</replaceable> [ (<replaceable>column_name</replaceable> [, ...] ) ] [ USING <replaceable class="parameter">method</replaceable> ] [ WITH ( <replaceable class="parameter">storage_parameter</replaceable> [= <replaceable class="parameter">value</replaceable>] [, ... ] ) ] @@ -60,6 +60,17 @@ CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] <replaceable>table_name</replaceable> <title>Parameters</title> <variablelist> + <varlistentry> + <term><literal>OR REPLACE</literal></term> + <listitem> + <para> + Replaces a materialized view if it already exists. + Specifying <literal>OR REPLACE</literal> together with + <literal>IF NOT EXISTS</literal> is an error. + </para> + </listitem> + </varlistentry> + <varlistentry> <term><literal>IF NOT EXISTS</literal></term> <listitem> @@ -67,7 +78,7 @@ CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] <replaceable>table_name</replaceable> Do not throw an error if a materialized view with the same name already exists. A notice is issued in this case. Note that there is no guarantee that the existing materialized view is anything like the one that would - have been created. + have been created, unless you use <literal>OR REPLACE</literal> instead. </para> </listitem> </varlistentry> diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c index dfd2ab8e862..1620273f965 100644 --- a/src/backend/commands/createas.c +++ b/src/backend/commands/createas.c @@ -24,6 +24,7 @@ */ #include "postgres.h" +#include "miscadmin.h" #include "access/heapam.h" #include "access/reloptions.h" #include "access/tableam.h" @@ -34,6 +35,7 @@ #include "commands/matview.h" #include "commands/prepare.h" #include "commands/tablecmds.h" +#include "commands/tablespace.h" #include "commands/view.h" #include "executor/execdesc.h" #include "executor/executor.h" @@ -81,55 +83,161 @@ static void intorel_destroy(DestReceiver *self); static ObjectAddress create_ctas_internal(List *attrList, IntoClause *into) { - CreateStmt *create = makeNode(CreateStmt); - bool is_matview; + bool is_matview, + replace = false; char relkind; - Datum toast_options; - const char *const validnsps[] = HEAP_RELOPT_NAMESPACES; + Oid matviewOid = InvalidOid; ObjectAddress intoRelationAddr; /* This code supports both CREATE TABLE AS and CREATE MATERIALIZED VIEW */ is_matview = (into->viewQuery != NULL); relkind = is_matview ? RELKIND_MATVIEW : RELKIND_RELATION; - /* - * Create the target relation by faking up a CREATE TABLE parsetree and - * passing it to DefineRelation. - */ - create->relation = into->rel; - create->tableElts = attrList; - create->inhRelations = NIL; - create->ofTypename = NULL; - create->constraints = NIL; - create->options = into->options; - create->oncommit = into->onCommit; - create->tablespacename = into->tableSpaceName; - create->if_not_exists = false; - create->accessMethod = into->accessMethod; + /* Check if an existing materialized view needs to be replaced. */ + if (is_matview) + { + LOCKMODE lockmode; - /* - * Create the relation. (This will error out if there's an existing view, - * so we don't need more code to complain if "replace" is false.) - */ - intoRelationAddr = DefineRelation(create, relkind, InvalidOid, NULL, NULL); + lockmode = into->replace ? AccessExclusiveLock : NoLock; + (void) RangeVarGetAndCheckCreationNamespace(into->rel, lockmode, + &matviewOid); + replace = OidIsValid(matviewOid) && into->replace; + } - /* - * If necessary, create a TOAST table for the target table. Note that - * NewRelationCreateToastTable ends with CommandCounterIncrement(), so - * that the TOAST table will be visible for insertion. - */ - CommandCounterIncrement(); + if (is_matview && replace) + { + Relation rel; + List *atcmds = NIL; + AlterTableCmd *atcmd; + TupleDesc descriptor; + + rel = relation_open(matviewOid, NoLock); + + if (rel->rd_rel->relkind != RELKIND_MATVIEW) + ereport(ERROR, + errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("\"%s\" is not a materialized view", + RelationGetRelationName(rel))); + + CheckTableNotInUse(rel, "CREATE OR REPLACE MATERIALIZED VIEW"); + + descriptor = BuildDescForRelation(attrList); + checkViewColumns(descriptor, rel->rd_att, true); + + /* add new attributes */ + if (list_length(attrList) > rel->rd_att->natts) + { + ListCell *c; + int skip = rel->rd_att->natts; + + foreach(c, attrList) + { + if (skip > 0) + { + skip--; + continue; + } + atcmd = makeNode(AlterTableCmd); + atcmd->subtype = AT_AddColumnToView; + atcmd->def = (Node *) lfirst(c); + atcmds = lappend(atcmds, atcmd); + } + } + + /* + * The following alters access method, tablespace, and storage options. + * When replacing an existing matview we need to alter the relation + * such that the defaults apply as if they have not been specified at + * all by the CREATE statement. + */ + + /* access method */ + atcmd = makeNode(AlterTableCmd); + atcmd->subtype = AT_SetAccessMethod; + atcmd->name = into->accessMethod ? into->accessMethod : default_table_access_method; + atcmds = lappend(atcmds, atcmd); + + /* tablespace */ + atcmd = makeNode(AlterTableCmd); + atcmd->subtype = AT_SetTableSpace; + if (into->tableSpaceName != NULL) + atcmd->name = into->tableSpaceName; + else + { + Oid spcid; + + /* + * Resolve the name of the default or database tablespace because + * we need to specify the tablespace by name. + * + * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then. + */ + spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false); + if (!OidIsValid(spcid)) + spcid = MyDatabaseTableSpace; + atcmd->name = get_tablespace_name(spcid); + } + atcmds = lappend(atcmds, atcmd); + + /* storage options */ + atcmd = makeNode(AlterTableCmd); + atcmd->subtype = AT_ReplaceRelOptions; + atcmd->def = (Node *) into->options; + atcmds = lappend(atcmds, atcmd); + + AlterTableInternal(matviewOid, atcmds, true); + CommandCounterIncrement(); + + relation_close(rel, NoLock); + ObjectAddressSet(intoRelationAddr, RelationRelationId, matviewOid); + } + else + { + CreateStmt *create = makeNode(CreateStmt); + Datum toast_options; + const static char *validnsps[] = HEAP_RELOPT_NAMESPACES; + + /* + * Create the target relation by faking up a CREATE TABLE parsetree + * and passing it to DefineRelation. + */ + create->relation = into->rel; + create->tableElts = attrList; + create->inhRelations = NIL; + create->ofTypename = NULL; + create->constraints = NIL; + create->options = into->options; + create->oncommit = into->onCommit; + create->tablespacename = into->tableSpaceName; + create->if_not_exists = false; + create->accessMethod = into->accessMethod; + + /* + * Create the relation. (This will error out if there's an existing + * view, so we don't need more code to complain if "replace" is + * false.) + */ + intoRelationAddr = DefineRelation(create, relkind, InvalidOid, NULL, + NULL); - /* parse and validate reloptions for the toast table */ - toast_options = transformRelOptions((Datum) 0, - create->options, - "toast", - validnsps, - true, false); + /* + * If necessary, create a TOAST table for the target table. Note that + * NewRelationCreateToastTable ends with CommandCounterIncrement(), so + * that the TOAST table will be visible for insertion. + */ + CommandCounterIncrement(); + + /* parse and validate reloptions for the toast table */ + toast_options = transformRelOptions((Datum) 0, + create->options, + "toast", + validnsps, + true, false); - (void) heap_reloptions(RELKIND_TOASTVALUE, toast_options, true); + (void) heap_reloptions(RELKIND_TOASTVALUE, toast_options, true); - NewRelationCreateToastTable(intoRelationAddr.objectId, toast_options); + NewRelationCreateToastTable(intoRelationAddr.objectId, toast_options); + } /* Create the "view" part of a materialized view. */ if (is_matview) @@ -137,7 +245,7 @@ create_ctas_internal(List *attrList, IntoClause *into) /* StoreViewQuery scribbles on tree, so make a copy */ Query *query = copyObject(into->viewQuery); - StoreViewQuery(intoRelationAddr.objectId, query, false); + StoreViewQuery(intoRelationAddr.objectId, query, replace); CommandCounterIncrement(); } @@ -234,7 +342,26 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt, /* Check if the relation exists or not */ if (CreateTableAsRelExists(stmt)) + { + /* An existing materialized view can be replaced. */ + if (is_matview && into->replace) + { + RefreshMatViewStmt *refresh; + + /* Change the relation to match the new query and other options. */ + (void) create_ctas_nodata(query->targetList, into); + + /* Refresh the materialized view with a fake statement. */ + refresh = makeNode(RefreshMatViewStmt); + refresh->relation = into->rel; + refresh->skipData = into->skipData; + refresh->concurrent = false; + + return ExecRefreshMatView(refresh, pstate->p_sourcetext, qc); + } + return InvalidObjectAddress; + } /* * Create the tuple receiver object and insert info it will need @@ -402,14 +529,15 @@ CreateTableAsRelExists(CreateTableAsStmt *ctas) oldrelid = get_relname_relid(into->rel->relname, nspid); if (OidIsValid(oldrelid)) { - if (!ctas->if_not_exists) + if (!ctas->if_not_exists && !into->replace) ereport(ERROR, (errcode(ERRCODE_DUPLICATE_TABLE), errmsg("relation \"%s\" already exists", into->rel->relname))); /* - * The relation exists and IF NOT EXISTS has been specified. + * The relation exists and IF NOT EXISTS or OR REPLACE has been + * specified. * * If we are in an extension script, insist that the pre-existing * object be a member of the extension, to avoid security risks. @@ -417,11 +545,12 @@ CreateTableAsRelExists(CreateTableAsStmt *ctas) ObjectAddressSet(address, RelationRelationId, oldrelid); checkMembershipInCurrentExtension(&address); - /* OK to skip */ - ereport(NOTICE, - (errcode(ERRCODE_DUPLICATE_TABLE), - errmsg("relation \"%s\" already exists, skipping", - into->rel->relname))); + if (ctas->if_not_exists) + /* OK to skip */ + ereport(NOTICE, + (errcode(ERRCODE_DUPLICATE_TABLE), + errmsg("relation \"%s\" already exists, skipping", + into->rel->relname))); return true; } diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index cb811520c29..44dcd2c5b0d 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -4645,7 +4645,7 @@ AlterTableGetLockLevel(List *cmds) * Subcommands that may be visible to concurrent SELECTs */ case AT_DropColumn: /* change visible to SELECT */ - case AT_AddColumnToView: /* CREATE VIEW */ + case AT_AddColumnToView: /* via CREATE OR REPLACE [MATERIALIZED] VIEW */ case AT_DropOids: /* used to equiv to DropColumn */ case AT_EnableAlwaysRule: /* may change SELECT rules */ case AT_EnableReplicaRule: /* may change SELECT rules */ @@ -4940,8 +4940,8 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd, /* Recursion occurs during execution phase */ pass = AT_PASS_ADD_COL; break; - case AT_AddColumnToView: /* add column via CREATE OR REPLACE VIEW */ - ATSimplePermissions(cmd->subtype, rel, ATT_VIEW); + case AT_AddColumnToView: /* via CREATE OR REPLACE [MATERIALIZED] VIEW */ + ATSimplePermissions(cmd->subtype, rel, ATT_VIEW | ATT_MATVIEW); ATPrepAddColumn(wqueue, rel, recurse, recursing, true, cmd, lockmode, context); /* Recursion occurs during execution phase */ @@ -5372,7 +5372,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, switch (cmd->subtype) { case AT_AddColumn: /* ADD COLUMN */ - case AT_AddColumnToView: /* add column via CREATE OR REPLACE VIEW */ + case AT_AddColumnToView: /* via CREATE OR REPLACE [MATERIALIZED] VIEW */ address = ATExecAddColumn(wqueue, tab, rel, &cmd, cmd->recurse, false, lockmode, cur_pass, context); diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c index 6f0301555e0..53d4cacc4c1 100644 --- a/src/backend/commands/view.c +++ b/src/backend/commands/view.c @@ -30,8 +30,6 @@ #include "utils/lsyscache.h" #include "utils/rel.h" -static void checkViewColumns(TupleDesc newdesc, TupleDesc olddesc); - /*--------------------------------------------------------------------- * DefineVirtualRelation * @@ -129,7 +127,7 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, * column list. */ descriptor = BuildDescForRelation(attrList); - checkViewColumns(descriptor, rel->rd_att); + checkViewColumns(descriptor, rel->rd_att, false); /* * If new attributes have been added, we must add pg_attribute entries @@ -263,15 +261,22 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, * added to generate specific complaints. Also, we allow the new view to have * more columns than the old. */ -static void -checkViewColumns(TupleDesc newdesc, TupleDesc olddesc) +void +checkViewColumns(TupleDesc newdesc, TupleDesc olddesc, bool is_matview) { int i; if (newdesc->natts < olddesc->natts) - ereport(ERROR, - (errcode(ERRCODE_INVALID_TABLE_DEFINITION), - errmsg("cannot drop columns from view"))); + { + if (is_matview) + ereport(ERROR, + errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot drop columns from materialized view")); + else + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot drop columns from view"))); + } for (i = 0; i < olddesc->natts; i++) { @@ -280,17 +285,34 @@ checkViewColumns(TupleDesc newdesc, TupleDesc olddesc) /* XXX msg not right, but we don't support DROP COL on view anyway */ if (newattr->attisdropped != oldattr->attisdropped) - ereport(ERROR, - (errcode(ERRCODE_INVALID_TABLE_DEFINITION), - errmsg("cannot drop columns from view"))); + { + if (is_matview) + ereport(ERROR, + errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot drop columns from materialized view")); + else + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot drop columns from view"))); + } if (strcmp(NameStr(newattr->attname), NameStr(oldattr->attname)) != 0) - ereport(ERROR, - (errcode(ERRCODE_INVALID_TABLE_DEFINITION), - errmsg("cannot change name of view column \"%s\" to \"%s\"", - NameStr(oldattr->attname), - NameStr(newattr->attname)), - errhint("Use ALTER VIEW ... RENAME COLUMN ... to change name of view column instead."))); + { + if (is_matview) + ereport(ERROR, + errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot change name of materialized view column \"%s\" to \"%s\"", + NameStr(oldattr->attname), + NameStr(newattr->attname)), + errhint("Use ALTER MATERIALIZED VIEW ... RENAME COLUMN ... to change name of materialized view column instead.")); + else + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot change name of view column \"%s\" to \"%s\"", + NameStr(oldattr->attname), + NameStr(newattr->attname)), + errhint("Use ALTER VIEW ... RENAME COLUMN ... to change name of view column instead."))); + } /* * We cannot allow type, typmod, or collation to change, since these @@ -299,26 +321,48 @@ checkViewColumns(TupleDesc newdesc, TupleDesc olddesc) */ if (newattr->atttypid != oldattr->atttypid || newattr->atttypmod != oldattr->atttypmod) - ereport(ERROR, - (errcode(ERRCODE_INVALID_TABLE_DEFINITION), - errmsg("cannot change data type of view column \"%s\" from %s to %s", - NameStr(oldattr->attname), - format_type_with_typemod(oldattr->atttypid, - oldattr->atttypmod), - format_type_with_typemod(newattr->atttypid, - newattr->atttypmod)))); + { + if (is_matview) + ereport(ERROR, + errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot change data type of materialized view column \"%s\" from %s to %s", + NameStr(oldattr->attname), + format_type_with_typemod(oldattr->atttypid, + oldattr->atttypmod), + format_type_with_typemod(newattr->atttypid, + newattr->atttypmod))); + else + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot change data type of view column \"%s\" from %s to %s", + NameStr(oldattr->attname), + format_type_with_typemod(oldattr->atttypid, + oldattr->atttypmod), + format_type_with_typemod(newattr->atttypid, + newattr->atttypmod)))); + } /* * At this point, attcollations should be both valid or both invalid, * so applying get_collation_name unconditionally should be fine. */ if (newattr->attcollation != oldattr->attcollation) - ereport(ERROR, - (errcode(ERRCODE_INVALID_TABLE_DEFINITION), - errmsg("cannot change collation of view column \"%s\" from \"%s\" to \"%s\"", - NameStr(oldattr->attname), - get_collation_name(oldattr->attcollation), - get_collation_name(newattr->attcollation)))); + { + if (is_matview) + ereport(ERROR, + errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot change collation of materialized view column \"%s\" from \"%s\" to \"%s\"", + NameStr(oldattr->attname), + get_collation_name(oldattr->attcollation), + get_collation_name(newattr->attcollation))); + else + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("cannot change collation of view column \"%s\" from \"%s\" to \"%s\"", + NameStr(oldattr->attname), + get_collation_name(oldattr->attcollation), + get_collation_name(newattr->attcollation)))); + } } /* diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index db43034b9db..7aaf0e37ad8 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -4929,6 +4929,21 @@ CreateMatViewStmt: $8->skipData = !($11); $$ = (Node *) ctas; } + | CREATE OR REPLACE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data + { + CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt); + + ctas->query = $9; + ctas->into = $7; + ctas->objtype = OBJECT_MATVIEW; + ctas->is_select_into = false; + ctas->if_not_exists = false; + /* cram additional flags into the IntoClause */ + $7->rel->relpersistence = $4; + $7->skipData = !($10); + $7->replace = true; + $$ = (Node *) ctas; + } ; create_mv_target: diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c index 1f2ca946fc5..f49f0b60c95 100644 --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -2187,7 +2187,7 @@ match_previous_words(int pattern_id, /* complete with something you can create or replace */ else if (TailMatches("CREATE", "OR", "REPLACE")) COMPLETE_WITH("FUNCTION", "PROCEDURE", "LANGUAGE", "RULE", "VIEW", - "AGGREGATE", "TRANSFORM", "TRIGGER"); + "AGGREGATE", "TRANSFORM", "TRIGGER", "MATERIALIZED VIEW"); /* DROP, but not DROP embedded in other commands */ /* complete with something you can drop */ @@ -4072,28 +4072,34 @@ match_previous_words(int pattern_id, COMPLETE_WITH("SELECT"); /* CREATE MATERIALIZED VIEW */ - else if (Matches("CREATE", "MATERIALIZED")) + else if (Matches("CREATE", "MATERIALIZED") || + Matches("CREATE", "OR", "REPLACE", "MATERIALIZED")) COMPLETE_WITH("VIEW"); - /* Complete CREATE MATERIALIZED VIEW <name> with AS or USING */ - else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny)) + /* Complete CREATE [ OR REPLACE ] MATERIALIZED VIEW <name> with AS or USING */ + else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny) || + Matches("CREATE", "OR", "REPLACE", "MATERIALIZED", "VIEW", MatchAny)) COMPLETE_WITH("AS", "USING"); /* - * Complete CREATE MATERIALIZED VIEW <name> USING with list of access + * Complete CREATE [ OR REPLACE ] MATERIALIZED VIEW <name> USING with list of access * methods */ - else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING")) + else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING") || + Matches("CREATE", "OR", "REPLACE", "MATERIALIZED", "VIEW", MatchAny, "USING")) COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods); - /* Complete CREATE MATERIALIZED VIEW <name> USING <access method> with AS */ - else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny)) + /* Complete CREATE [ OR REPLACE ] MATERIALIZED VIEW <name> USING <access method> with AS */ + else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny) || + Matches("CREATE", "OR", "REPLACE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny)) COMPLETE_WITH("AS"); /* - * Complete CREATE MATERIALIZED VIEW <name> [USING <access method> ] AS + * Complete CREATE [ OR REPLACE ] MATERIALIZED VIEW <name> [USING <access method> ] AS * with "SELECT" */ else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "AS") || - Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny, "AS")) + Matches("CREATE", "OR", "REPLACE", "MATERIALIZED", "VIEW", MatchAny, "AS") || + Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny, "AS") || + Matches("CREATE", "OR", "REPLACE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny, "AS")) COMPLETE_WITH("SELECT"); /* CREATE EVENT TRIGGER */ diff --git a/src/include/commands/view.h b/src/include/commands/view.h index c41f51b161c..95290f0a1c4 100644 --- a/src/include/commands/view.h +++ b/src/include/commands/view.h @@ -22,4 +22,7 @@ extern ObjectAddress DefineView(ViewStmt *stmt, const char *queryString, extern void StoreViewQuery(Oid viewOid, Query *viewParse, bool replace); +extern void checkViewColumns(TupleDesc newdesc, TupleDesc olddesc, + bool is_matview); + #endif /* VIEW_H */ diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 86a236bd58b..74922f8ae89 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -2412,7 +2412,7 @@ typedef struct AlterTableStmt typedef enum AlterTableType { AT_AddColumn, /* add column */ - AT_AddColumnToView, /* implicitly via CREATE OR REPLACE VIEW */ + AT_AddColumnToView, /* implicitly via CREATE OR REPLACE [MATERIALIZED] VIEW */ AT_ColumnDefault, /* alter column default */ AT_CookedColumnDefault, /* add a pre-cooked column default */ AT_DropNotNull, /* alter column drop not null */ diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 6dfca3cb35b..3a0c3b06c81 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -169,6 +169,7 @@ typedef struct IntoClause /* materialized view's SELECT query */ struct Query *viewQuery pg_node_attr(query_jumble_ignore); bool skipData; /* true for WITH NO DATA */ + bool replace; /* replace existing matview? */ } IntoClause; diff --git a/src/test/regress/expected/matview.out b/src/test/regress/expected/matview.out index c56c9fa3a25..56104b7ee8b 100644 --- a/src/test/regress/expected/matview.out +++ b/src/test/regress/expected/matview.out @@ -694,3 +694,208 @@ NOTICE: relation "matview_ine_tab" already exists, skipping (0 rows) DROP MATERIALIZED VIEW matview_ine_tab; +-- +-- test CREATE OR REPLACE MATERIALIZED VIEW +-- +-- matview does not already exist +DROP MATERIALIZED VIEW IF EXISTS mvtest_replace; +NOTICE: materialized view "mvtest_replace" does not exist, skipping +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 1 AS a; +SELECT * FROM mvtest_replace; + a +--- + 1 +(1 row) + +-- replace query with data +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 2 AS a; +SELECT * FROM mvtest_replace; + a +--- + 2 +(1 row) + +-- replace query without data +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 3 AS a + WITH NO DATA; +SELECT * FROM mvtest_replace; -- error: not populated +ERROR: materialized view "mvtest_replace" has not been populated +HINT: Use the REFRESH MATERIALIZED VIEW command. +REFRESH MATERIALIZED VIEW mvtest_replace; +SELECT * FROM mvtest_replace; + a +--- + 3 +(1 row) + +-- add column +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 4 AS a, 1 b; +SELECT * FROM mvtest_replace; + a | b +---+--- + 4 | 1 +(1 row) + +-- replace table options +SELECT m.*, c.relname, c.reloptions, s.spcname, a.amname + FROM mvtest_replace m + CROSS JOIN pg_class c + LEFT JOIN pg_tablespace s ON s.oid = c.reltablespace + LEFT JOIN pg_am a ON a.oid = c.relam + WHERE c.relname = 'mvtest_replace'; + a | b | relname | reloptions | spcname | amname +---+---+----------------+------------+---------+-------- + 4 | 1 | mvtest_replace | | | heap +(1 row) + +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace + USING heap2 + WITH (fillfactor = 50) + TABLESPACE regress_tblspace + AS SELECT 5 AS a, 1 AS b; +SELECT m.*, c.relname, c.reloptions, s.spcname, a.amname + FROM mvtest_replace m + CROSS JOIN pg_class c + LEFT JOIN pg_tablespace s ON s.oid = c.reltablespace + LEFT JOIN pg_am a ON a.oid = c.relam + WHERE c.relname = 'mvtest_replace'; + a | b | relname | reloptions | spcname | amname +---+---+----------------+-----------------+------------------+-------- + 5 | 1 | mvtest_replace | {fillfactor=50} | regress_tblspace | heap2 +(1 row) + +-- restore default options +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace + AS SELECT 5 AS a, 1 AS b; +SELECT m.*, c.relname, c.reloptions, s.spcname, a.amname + FROM mvtest_replace m + CROSS JOIN pg_class c + LEFT JOIN pg_tablespace s ON s.oid = c.reltablespace + LEFT JOIN pg_am a ON a.oid = c.relam + WHERE c.relname = 'mvtest_replace'; + a | b | relname | reloptions | spcname | amname +---+---+----------------+------------+---------+-------- + 5 | 1 | mvtest_replace | | | heap +(1 row) + +-- can replace matview that has a dependent view +CREATE VIEW mvtest_replace_v AS + SELECT * FROM mvtest_replace; +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 6 AS a, 1 AS b; +SELECT * FROM mvtest_replace, mvtest_replace_v; + a | b | a | b +---+---+---+--- + 6 | 1 | 6 | 1 +(1 row) + +DROP VIEW mvtest_replace_v; +-- index gets rebuilt when replacing with data +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 7 AS a, 1 AS b; +CREATE UNIQUE INDEX ON mvtest_replace (b); +SELECT * FROM mvtest_replace; + a | b +---+--- + 7 | 1 +(1 row) + +SET enable_seqscan = off; -- force index scan +EXPLAIN (COSTS OFF) SELECT * FROM mvtest_replace WHERE b = 1; + QUERY PLAN +--------------------------------------------------------- + Index Scan using mvtest_replace_b_idx on mvtest_replace + Index Cond: (b = 1) +(2 rows) + +SELECT * FROM mvtest_replace WHERE b = 1; + a | b +---+--- + 7 | 1 +(1 row) + +RESET enable_seqscan; +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 8 AS a, 1 AS b; +SET enable_seqscan = off; -- force index scan +EXPLAIN (COSTS OFF) SELECT * FROM mvtest_replace WHERE b = 1; + QUERY PLAN +--------------------------------------------------------- + Index Scan using mvtest_replace_b_idx on mvtest_replace + Index Cond: (b = 1) +(2 rows) + +SELECT * FROM mvtest_replace WHERE b = 1; + a | b +---+--- + 8 | 1 +(1 row) + +RESET enable_seqscan; +-- cannot change column data type +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 9 AS a, 'x' AS b; -- error +ERROR: cannot change data type of materialized view column "b" from integer to text +SELECT * FROM mvtest_replace; + a | b +---+--- + 8 | 1 +(1 row) + +-- cannot rename column +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 10 AS a, 1 AS b2; -- error +ERROR: cannot change name of materialized view column "b" to "b2" +HINT: Use ALTER MATERIALIZED VIEW ... RENAME COLUMN ... to change name of materialized view column instead. +SELECT * FROM mvtest_replace; + a | b +---+--- + 8 | 1 +(1 row) + +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 11 AS a, 1 AS b, 'y' COLLATE "C" AS c; +SELECT * FROM mvtest_replace; + a | b | c +----+---+--- + 11 | 1 | y +(1 row) + +-- cannot change column collation +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 12 AS a, 1 AS b, 'x' COLLATE "POSIX" AS c; -- error +ERROR: cannot change collation of materialized view column "c" from "C" to "POSIX" +SELECT * FROM mvtest_replace; + a | b | c +----+---+--- + 11 | 1 | y +(1 row) + +-- cannot drop column +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 13 AS a, 1 AS b; -- error +ERROR: cannot drop columns from materialized view +SELECT * FROM mvtest_replace; + a | b | c +----+---+--- + 11 | 1 | y +(1 row) + +-- must target a matview +CREATE VIEW mvtest_not_mv AS + SELECT 1 AS a; +CREATE OR REPLACE MATERIALIZED VIEW mvtest_not_mv AS + SELECT 1 AS a; -- error +ERROR: "mvtest_not_mv" is not a materialized view +DROP VIEW mvtest_not_mv; +-- cannot use OR REPLACE with IF NOT EXISTS +CREATE OR REPLACE MATERIALIZED VIEW IF NOT EXISTS mvtest_replace AS + SELECT 1 AS a; +ERROR: syntax error at or near "NOT" +LINE 1: CREATE OR REPLACE MATERIALIZED VIEW IF NOT EXISTS mvtest_rep... + ^ +DROP MATERIALIZED VIEW mvtest_replace; diff --git a/src/test/regress/sql/matview.sql b/src/test/regress/sql/matview.sql index 6704eeae2df..fe0cb4d25bf 100644 --- a/src/test/regress/sql/matview.sql +++ b/src/test/regress/sql/matview.sql @@ -314,3 +314,120 @@ EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF) CREATE MATERIALIZED VIEW IF NOT EXISTS matview_ine_tab AS SELECT 1 / 0 WITH NO DATA; -- ok DROP MATERIALIZED VIEW matview_ine_tab; + +-- +-- test CREATE OR REPLACE MATERIALIZED VIEW +-- + +-- matview does not already exist +DROP MATERIALIZED VIEW IF EXISTS mvtest_replace; +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 1 AS a; +SELECT * FROM mvtest_replace; + +-- replace query with data +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 2 AS a; +SELECT * FROM mvtest_replace; + +-- replace query without data +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 3 AS a + WITH NO DATA; +SELECT * FROM mvtest_replace; -- error: not populated +REFRESH MATERIALIZED VIEW mvtest_replace; +SELECT * FROM mvtest_replace; + +-- add column +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 4 AS a, 1 b; +SELECT * FROM mvtest_replace; + +-- replace table options +SELECT m.*, c.relname, c.reloptions, s.spcname, a.amname + FROM mvtest_replace m + CROSS JOIN pg_class c + LEFT JOIN pg_tablespace s ON s.oid = c.reltablespace + LEFT JOIN pg_am a ON a.oid = c.relam + WHERE c.relname = 'mvtest_replace'; +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace + USING heap2 + WITH (fillfactor = 50) + TABLESPACE regress_tblspace + AS SELECT 5 AS a, 1 AS b; +SELECT m.*, c.relname, c.reloptions, s.spcname, a.amname + FROM mvtest_replace m + CROSS JOIN pg_class c + LEFT JOIN pg_tablespace s ON s.oid = c.reltablespace + LEFT JOIN pg_am a ON a.oid = c.relam + WHERE c.relname = 'mvtest_replace'; +-- restore default options +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace + AS SELECT 5 AS a, 1 AS b; +SELECT m.*, c.relname, c.reloptions, s.spcname, a.amname + FROM mvtest_replace m + CROSS JOIN pg_class c + LEFT JOIN pg_tablespace s ON s.oid = c.reltablespace + LEFT JOIN pg_am a ON a.oid = c.relam + WHERE c.relname = 'mvtest_replace'; + +-- can replace matview that has a dependent view +CREATE VIEW mvtest_replace_v AS + SELECT * FROM mvtest_replace; +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 6 AS a, 1 AS b; +SELECT * FROM mvtest_replace, mvtest_replace_v; +DROP VIEW mvtest_replace_v; + +-- index gets rebuilt when replacing with data +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 7 AS a, 1 AS b; +CREATE UNIQUE INDEX ON mvtest_replace (b); +SELECT * FROM mvtest_replace; +SET enable_seqscan = off; -- force index scan +EXPLAIN (COSTS OFF) SELECT * FROM mvtest_replace WHERE b = 1; +SELECT * FROM mvtest_replace WHERE b = 1; +RESET enable_seqscan; +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 8 AS a, 1 AS b; +SET enable_seqscan = off; -- force index scan +EXPLAIN (COSTS OFF) SELECT * FROM mvtest_replace WHERE b = 1; +SELECT * FROM mvtest_replace WHERE b = 1; +RESET enable_seqscan; + +-- cannot change column data type +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 9 AS a, 'x' AS b; -- error +SELECT * FROM mvtest_replace; + +-- cannot rename column +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 10 AS a, 1 AS b2; -- error +SELECT * FROM mvtest_replace; + +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 11 AS a, 1 AS b, 'y' COLLATE "C" AS c; +SELECT * FROM mvtest_replace; + +-- cannot change column collation +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 12 AS a, 1 AS b, 'x' COLLATE "POSIX" AS c; -- error +SELECT * FROM mvtest_replace; + +-- cannot drop column +CREATE OR REPLACE MATERIALIZED VIEW mvtest_replace AS + SELECT 13 AS a, 1 AS b; -- error +SELECT * FROM mvtest_replace; + +-- must target a matview +CREATE VIEW mvtest_not_mv AS + SELECT 1 AS a; +CREATE OR REPLACE MATERIALIZED VIEW mvtest_not_mv AS + SELECT 1 AS a; -- error +DROP VIEW mvtest_not_mv; + +-- cannot use OR REPLACE with IF NOT EXISTS +CREATE OR REPLACE MATERIALIZED VIEW IF NOT EXISTS mvtest_replace AS + SELECT 1 AS a; + +DROP MATERIALIZED VIEW mvtest_replace; -- 2.50.1 --r33ycpluvyfavkwy Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v7-0002-Handle-default-tablespace-in-AlterTableInternal.patch" ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
* [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. @ 2026-06-02 09:37 Andy Fan <zhihuifan1213@163.com> 0 siblings, 0 replies; 513+ messages in thread From: Andy Fan @ 2026-06-02 09:37 UTC (permalink / raw) --- src/backend/access/common/printtup.c | 8 ++- src/backend/utils/adt/date.c | 32 +++++++---- src/backend/utils/adt/float.c | 62 ++++++++++++++++----- src/backend/utils/adt/int.c | 82 +++++++++++++++------------- src/backend/utils/adt/int8.c | 40 ++++++++++---- src/backend/utils/adt/numeric.c | 46 ++++++++++++---- src/backend/utils/adt/timestamp.c | 50 ++++++++++++----- src/backend/utils/adt/varlena.c | 52 ++++++++---------- src/include/libpq/pqformat.h | 61 +++++++++++++++++++++ src/include/utils/builtins.h | 24 ++++++++ 10 files changed, 325 insertions(+), 132 deletions(-) diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index 2e3eb8f56d3..ab625736ac1 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -393,17 +393,19 @@ printtup(TupleTableSlot *slot, DestReceiver *self) else { /* Binary output */ + Datum outputdatum; bytea *outputbytes; - outputbytes = DatumGetByteaP(FunctionCallInvoke(thisState->outstate)); + outputdatum = FunctionCallInvoke(thisState->outstate); Assert(!thisState->outstate->isnull); /* - * If outputbytes == NULL, the send function directly appended a + * If outputdatum == 0, the send function directly appended a * correctly formatted message. */ - if (outputbytes) + if (outputdatum) { + outputbytes = DatumGetByteaP(outputdatum); pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ); pq_sendbytes(buf, VARDATA(outputbytes), VARSIZE(outputbytes) - VARHDRSZ); diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..43284d43fda 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -180,7 +180,6 @@ Datum date_out(PG_FUNCTION_ARGS) { DateADT date = PG_GETARG_DATEADT(0); - char *result; struct pg_tm tt, *tm = &tt; char buf[MAXDATELEN + 1]; @@ -194,8 +193,10 @@ date_out(PG_FUNCTION_ARGS) EncodeDateOnly(tm, DateStyle, buf); } - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1606,7 +1607,6 @@ Datum time_out(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -1615,8 +1615,10 @@ time_out(PG_FUNCTION_ARGS) time2tm(time, tm, &fsec); EncodeTimeOnly(tm, fsec, false, 0, DateStyle, buf); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -1652,11 +1654,21 @@ Datum time_send(PG_FUNCTION_ARGS) { TimeADT time = PG_GETARG_TIMEADT(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, time); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, time); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, time); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 362c29ab803..161d5593f5f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -24,6 +24,7 @@ #include "common/shortest_dec.h" #include "libpq/pqformat.h" #include "utils/array.h" +#include "utils/builtins.h" #include "utils/float.h" #include "utils/fmgrprotos.h" #include "utils/sortsupport.h" @@ -360,17 +361,18 @@ Datum float4out(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - char *ascii = (char *) palloc(32); + char ascii[32]; int ndig = FLT_DIG + extra_float_digits; if (extra_float_digits > 0) - { float_to_shortest_decimal_buf(num, ascii); - PG_RETURN_CSTRING(ascii); - } + else + (void) pg_strfromd(ascii, 32, ndig, num); - (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(ascii); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -391,11 +393,21 @@ Datum float4send(PG_FUNCTION_ARGS) { float4 num = PG_GETARG_FLOAT4(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat4(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat4_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat4(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -563,8 +575,18 @@ Datum float8out(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); + char ascii[32]; + int ndig = DBL_DIG + extra_float_digits; + + if (extra_float_digits > 0) + double_to_shortest_decimal_buf(num, ascii); + else + (void) pg_strfromd(ascii, 32, ndig, num); - PG_RETURN_CSTRING(float8out_internal(num)); + if (pg_send_inout_text(fcinfo, ascii, strlen(ascii))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(ascii)); } /* @@ -608,11 +630,21 @@ Datum float8send(PG_FUNCTION_ARGS) { float8 num = PG_GETARG_FLOAT8(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendfloat8(&buf, num); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendfloat8_field(buf, num); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendfloat8(&buf, num); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index de54a43c98d..9a3fda0403a 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -74,10 +74,27 @@ Datum int2out(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - char *result = (char *) palloc(7); /* sign, 5 digits, '\0' */ + int maxlen = 7; /* sign, 5 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pg_itoa(arg1, result); - PG_RETURN_CSTRING(result); + if (buf) + { + int offset; + int len; + + len = pg_itoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char *result = (char *) palloc(maxlen); + + pg_itoa(arg1, result); + PG_RETURN_CSTRING(result); + } } /* @@ -98,11 +115,21 @@ Datum int2send(PG_FUNCTION_ARGS) { int16 arg1 = PG_GETARG_INT16(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint16(&buf, arg1); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint16_field(buf, arg1); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint16(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } /* @@ -328,40 +355,22 @@ int4out(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); int maxlen = 12; /* sign, 10 digits, '\0' */ + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - /* - * Optimized path for output functions called as part of a larger - * ouput. - * - * FIXME: A good chunk of this should obviously be in helper - * functions. - */ - InOutContext *inout = castNode(InOutContext, fcinfo->context); - StringInfo buf = inout->buf; - int prev_buflen; + /* Optimized path for output functions called as part of a larger output. */ + int offset; int len; - uint32 len_net; - - /* reserve space for length and the max string length */ - enlargeStringInfo(buf, sizeof(uint32) + maxlen); - - /* reserve space for length, to be filled out later */ - prev_buflen = buf->len; - buf->len += sizeof(uint32); /* * Construct string directly in buffer, we don't have to care about * encoding conversions, because we assume that every encoding * embodies ascii (XXX: Is that actually true with client encodings?). */ - len = pg_ltoa(arg1, buf->data + buf->len); + len = pg_ltoa(arg1, pq_begincountedfield(buf, maxlen, &offset)); buf->len += len; - - /* update the previously reserved length */ - len_net = pg_hton32(len); - memcpy(&buf->data[prev_buflen], &len_net, sizeof(uint32)); + pq_endcountedfield(buf, offset); PG_RETURN_VOID(); } @@ -395,16 +404,11 @@ Datum int4send(PG_FUNCTION_ARGS) { int32 arg1 = PG_GETARG_INT32(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - InOutContext *inout = castNode(InOutContext, fcinfo->context); - - /* length of data */ - pq_sendint32(inout->buf, 4); - /* data itself */ - pq_sendint32(inout->buf, arg1); - + pq_sendint32_field(buf, arg1); PG_RETURN_VOID(); } else diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 9b429da86d9..184927f4438 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -63,19 +63,37 @@ Datum int8out(PG_FUNCTION_ARGS) { int64 val = PG_GETARG_INT64(0); - char buf[MAXINT8LEN + 1]; - char *result; - int len; + int maxlen = MAXINT8LEN + 1; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - len = pg_lltoa(val, buf) + 1; + if (buf) + { + int offset; + int len; - /* - * Since the length is already known, we do a manual palloc() and memcpy() - * to avoid the strlen() call that would otherwise be done in pstrdup(). - */ - result = palloc(len); - memcpy(result, buf, len); - PG_RETURN_CSTRING(result); + len = pg_lltoa(val, pq_begincountedfield(buf, maxlen, &offset)); + buf->len += len; + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + char buf[MAXINT8LEN + 1]; + char *result; + int len; + + len = pg_lltoa(val, buf) + 1; + + /* + * Since the length is already known, we do a manual palloc() and + * memcpy() to avoid the strlen() call that would otherwise be done in + * pstrdup(). + */ + result = palloc(len); + memcpy(result, buf, len); + PG_RETURN_CSTRING(result); + } } /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index cb23dfe9b95..1264a0bb0ff 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -808,11 +808,16 @@ numeric_out(PG_FUNCTION_ARGS) if (NUMERIC_IS_SPECIAL(num)) { if (NUMERIC_IS_PINF(num)) - PG_RETURN_CSTRING(pstrdup("Infinity")); + str = "Infinity"; else if (NUMERIC_IS_NINF(num)) - PG_RETURN_CSTRING(pstrdup("-Infinity")); + str = "-Infinity"; else - PG_RETURN_CSTRING(pstrdup("NaN")); + str = "NaN"; + + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + + PG_RETURN_CSTRING(pstrdup(str)); } /* @@ -822,6 +827,9 @@ numeric_out(PG_FUNCTION_ARGS) str = get_str_from_var(&x); + if (pg_send_inout_text(fcinfo, str, strlen(str))) + PG_RETURN_VOID(); + PG_RETURN_CSTRING(str); } @@ -1147,21 +1155,37 @@ numeric_send(PG_FUNCTION_ARGS) { Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; - StringInfoData buf; + StringInfo buf; + StringInfoData localbuf; + bool inout; int i; init_var_from_num(num, &x); - pq_begintypsend(&buf); + buf = pg_get_inout_context_buf(fcinfo); + inout = buf != NULL; + if (inout) + { + /* length of data */ + pq_sendint32(buf, (4 + x.ndigits) * sizeof(int16)); + } + else + { + pq_begintypsend(&localbuf); + buf = &localbuf; + } - pq_sendint16(&buf, x.ndigits); - pq_sendint16(&buf, x.weight); - pq_sendint16(&buf, x.sign); - pq_sendint16(&buf, x.dscale); + pq_sendint16(buf, x.ndigits); + pq_sendint16(buf, x.weight); + pq_sendint16(buf, x.sign); + pq_sendint16(buf, x.dscale); for (i = 0; i < x.ndigits; i++) - pq_sendint16(&buf, x.digits[i]); + pq_sendint16(buf, x.digits[i]); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (inout) + PG_RETURN_VOID(); + else + PG_RETURN_BYTEA_P(pq_endtypsend(&localbuf)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a20e7ea1d11..0c0b7af700a 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -227,7 +227,6 @@ Datum timestamp_out(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - char *result; struct pg_tm tt, *tm = &tt; fsec_t fsec; @@ -242,8 +241,10 @@ timestamp_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -286,11 +287,21 @@ Datum timestamp_send(PG_FUNCTION_ARGS) { Timestamp timestamp = PG_GETARG_TIMESTAMP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum @@ -773,7 +784,6 @@ Datum timestamptz_out(PG_FUNCTION_ARGS) { TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0); - char *result; int tz; struct pg_tm tt, *tm = &tt; @@ -790,8 +800,10 @@ timestamptz_out(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - result = pstrdup(buf); - PG_RETURN_CSTRING(result); + if (pg_send_inout_text(fcinfo, buf, strlen(buf))) + PG_RETURN_VOID(); + else + PG_RETURN_CSTRING(pstrdup(buf)); } /* @@ -835,11 +847,21 @@ Datum timestamptz_send(PG_FUNCTION_ARGS) { TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendint64(&buf, timestamp); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + pq_sendint64_field(buf, timestamp); + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, timestamp); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } Datum diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 4948ced7dec..5dac5b39f11 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -289,37 +289,15 @@ Datum textout(PG_FUNCTION_ARGS) { Datum txt = PG_GETARG_DATUM(0); + StringInfo buf = pg_get_inout_context_buf(fcinfo); - if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + if (buf) { - StringInfo buf = castNode(InOutContext, fcinfo->context)->buf; text *tunpacked = pg_detoast_datum_packed(DatumGetPointer(txt)); int len = VARSIZE_ANY_EXHDR(tunpacked); char *data = VARDATA_ANY(tunpacked); - char *data_converted; - size_t data_len; - - /* - * Convert text output to the right encoding. For efficiency, this - * should really happen directly into buf. For that we would have to - * reserve space for the length first and fill it out after - * conversion. - * - * FIXME: Obviously we would need helpers for this too. - */ - data_converted = pg_server_to_client(data, len); - - if (data == data_converted) - data_len = len; - else - data_len = strlen(data_converted); - - /* length */ - pq_sendint32(buf, data_len); - - /* actual data */ - appendBinaryStringInfoNT(buf, data_converted, data_len); + pq_sendcountedtext(buf, data, len); if (tunpacked != DatumGetPointer(txt)) pfree(tunpacked); @@ -356,11 +334,27 @@ Datum textsend(PG_FUNCTION_ARGS) { text *t = PG_GETARG_TEXT_PP(0); - StringInfoData buf; + StringInfo buf = pg_get_inout_context_buf(fcinfo); - pq_begintypsend(&buf); - pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); - PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + if (buf) + { + int offset; + + /* reserve space for length, to be filled out after conversion */ + (void) pq_begincountedfield(buf, 0, &offset); + pq_sendtext(buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + pq_endcountedfield(buf, offset); + + PG_RETURN_VOID(); + } + else + { + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); + } } diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..d40f36b5fbc 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -94,6 +94,32 @@ pq_writeint64(StringInfoData *pg_restrict buf, uint64 i) buf->len += sizeof(uint64); } +/* + * Reserve a length-prefixed field in a StringInfo buffer, returning the + * location at which the payload should be written. The payload length is + * filled in by pq_endcountedfield(). + */ +static inline char * +pq_begincountedfield(StringInfo buf, int maxlen, int *offset) +{ + enlargeStringInfo(buf, sizeof(uint32) + maxlen); + + *offset = buf->len; + buf->len += sizeof(uint32); + + return buf->data + buf->len; +} + +/* Fill in the length of a field started by pq_begincountedfield(). */ +static inline void +pq_endcountedfield(StringInfo buf, int offset) +{ + int len = buf->len - offset - sizeof(uint32); + uint32 len_net = pg_hton32(len); + + memcpy(&buf->data[offset], &len_net, sizeof(uint32)); +} + /* * Append a null-terminated text string (with conversion) to a buffer with * preallocated space. @@ -187,6 +213,41 @@ pq_sendint(StringInfo buf, uint32 i, int b) } } +/* append length-prefixed binary fields to a StringInfo buffer */ +static inline void +pq_sendint16_field(StringInfo buf, uint16 i) +{ + pq_sendint32(buf, sizeof(uint16)); + pq_sendint16(buf, i); +} + +static inline void +pq_sendint32_field(StringInfo buf, uint32 i) +{ + pq_sendint32(buf, sizeof(uint32)); + pq_sendint32(buf, i); +} + +static inline void +pq_sendint64_field(StringInfo buf, uint64 i) +{ + pq_sendint32(buf, sizeof(uint64)); + pq_sendint64(buf, i); +} + +static inline void +pq_sendfloat4_field(StringInfo buf, float4 f) +{ + pq_sendint32(buf, sizeof(float4)); + pq_sendfloat4(buf, f); +} + +static inline void +pq_sendfloat8_field(StringInfo buf, float8 f) +{ + pq_sendint32(buf, sizeof(float8)); + pq_sendfloat8(buf, f); +} extern void pq_begintypsend(StringInfo buf); extern bytea *pq_endtypsend(StringInfo buf); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index b6a11bfa288..0bf228a746b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -15,12 +15,36 @@ #define BUILTINS_H #include "fmgr.h" +#include "libpq/pqformat.h" +#include "nodes/miscnodes.h" #include "nodes/nodes.h" #include "utils/fmgrprotos.h" /* Sign + the most decimal digits an 8-byte number could have */ #define MAXINT8LEN 20 +/* Helpers for output/send functions using InOutContext. */ +static inline StringInfo +pg_get_inout_context_buf(FunctionCallInfo fcinfo) +{ + if (fcinfo->context && IsA(fcinfo->context, InOutContext)) + return castNode(InOutContext, fcinfo->context)->buf; + + return NULL; +} + +static inline bool +pg_send_inout_text(FunctionCallInfo fcinfo, const char *str, int slen) +{ + StringInfo buf = pg_get_inout_context_buf(fcinfo); + + if (!buf) + return false; + + pq_sendcountedtext(buf, str, slen); + return true; +} + /* bool.c */ extern bool parse_bool(const char *value, bool *result); extern bool parse_bool_with_len(const char *value, size_t len, bool *result); -- 2.43.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v2-0004-Let-pgbench-support-binary-format.patch ^ permalink raw reply [nested|flat] 513+ messages in thread
end of thread, other threads:[~2026-06-02 09:37 UTC | newest] Thread overview: 513+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2024-05-21 16:35 [PATCH v7 1/3] Add OR REPLACE option to CREATE MATERIALIZED VIEW Erik Wienhold <ewie@ewie.name> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com> 2026-06-02 09:37 [PATCH v2 3/4] Optimize more data type for less memory copy with optional context. Andy Fan <zhihuifan1213@163.com>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox